
package Server;

import XMLParserS.ServerSaxParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ClientThread extends Thread{

    private Socket socket;
    private BufferedReader in= null;
    private OutputStream out = null;
    private FileParserI parser = null;


    String fileName = "temp.xml";


    ClientThread(Socket socket)
    {
        this.socket = socket;
        parser = new ServerSaxParser();
    }
    @Override

    public void run()
    {
         try
        {

             while(this != null){
            // Receive request
             System.out.println("Request from: "+socket);
             
                InputStream input = socket.getInputStream();
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                FileOutputStream wr = new FileOutputStream(new File(fileName) );

                byte[] buffer = new byte[socket.getReceiveBufferSize()];
                int bytesReceived = 0;
               
              
             /*while((bytesReceived =input.read(buffer)) >0 )
             {
                 wr.write(buffer,0,bytesReceived);
                    System.out.println("bytesRecived= "+ bytesReceived);
                    System.out.write(buffer,0,bytesReceived);
             }*/

            do{
                // Write to the file
                   bytesReceived = input.read(buffer);
                    wr.write(buffer,0,bytesReceived);
                    System.out.println("bytesRecived= "+ bytesReceived);
                    System.out.write(buffer,0,bytesReceived);
                
                  
                  }   while(bytesReceived  == socket.getReceiveBufferSize());
            
                  parser.list(this, fileName);



                //System.out.println("bytesRecived= "+ bytesReceived);

                //System.out.println("before send asnwer");
                //SendAnswer("localTemp.xml");
                //System.out.println("after send asnwer");
        }
        }
        catch(IOException e)
        {
            System.out.println("IOException"+ e);
        }
    }

    public void SendAnswer(String filename)
    {
        try
        {
            System.out.println("SendAnswer");
            out = socket.getOutputStream();
            out = new PrintStream(socket.getOutputStream());
            
            // Send file
            FileInputStream file = new FileInputStream(filename);
            byte[] buffer = new byte[socket.getSendBufferSize()];
            int bytesRead = 0;

            while ((bytesRead = file.read(buffer)) > 0)
            {
                out.write(buffer, 0, bytesRead);
                System.out.write(buffer,0,bytesRead);
               
            }
            out.flush();
            file.close();

        } catch (IOException ex) {
            Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
